package sunger.net.org.dropmenu;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import sunger.net.org.dropmenu.adapter.FilterAdapter;
import sunger.net.org.dropmenu.adapter.ShopAdapter;
import sunger.net.org.dropmenu.tab.CommonTabList;
import sunger.net.org.dropmenu.tab.listener.CustomTabEntity;

import org.net.sunger.widget.DropDownLayout;
import org.net.sunger.widget.MenuLayout;
import sunger.net.org.dropmenu.tab.listener.OnTabSelectListener;

import java.util.ArrayList;

/**
 * MainAbilitySlice
 */
public class MainAbilitySlice extends AbilitySlice {
    private final String[] mTitles = {"All Floor", "All Merchant", "All Category"};
    private final int[] mIconUnselectIds = {ResourceTable.Media_tab_floor_unselected,
            ResourceTable.Media_tab_category_unseleted, ResourceTable.Media_tab_sort_unseleted};
    private final int[] mIconSelectIds = {ResourceTable.Media_tab_floor_selected,
            ResourceTable.Media_tab_category_seleted, ResourceTable.Media_tab_floor_selected};
    private final String[] clothes = {"All", "Socks", "Hat", "Glove", "Briefs", "Jacket"};
    private final String[] floors = {"B1", "B2", "B3", "1F", "2F", "3F", "4F", "5F", "6F", "7F", "8F", "9F"};
    private final String[] sorts = {"All", "A-Z", "Z-A", "Near"};
    private final ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ArrayList<DirectionalLayout> layouts;
    private DropDownLayout dropDownLayout;
    private CommonTabList layout;
    private int index;
    private MenuLayout menuLayout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        dropDownLayout = (DropDownLayout) findComponentById(ResourceTable.Id_dropdown);
        layout = (CommonTabList) findComponentById(ResourceTable.Id_my);
        updateTabData();
        addLayout();
        setLayoutSelectListener();
        menuLayout = (MenuLayout) findComponentById(ResourceTable.Id_menuLayout);
        menuLayout.bindLayouts(layouts);
        ShopAdapter shopAdapter = new ShopAdapter(getContext());
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_recyclerView);
        listContainer.setItemProvider(shopAdapter);
    }

    private void setLayoutSelectListener() {
        layout.setSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                dropDownLayout.showMenuAt(position);
                index = position;
            }

            @Override
            public void onTabReselect(int position) {
                if (menuLayout.isShow()) {
                    dropDownLayout.closeMenu();
                } else {
                    dropDownLayout.showMenuAt(position);
                }
            }
        });
    }

    /**
     * 添加list
     */
    private void addLayout() {
        layouts = new ArrayList<>();
        DirectionalLayout layout1 = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_list_layout, null, false);
        DirectionalLayout layout2 = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_list_layout, null, false);
        DirectionalLayout layout3 = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_list_layout, null, false);
        ListContainer container1
                = (ListContainer) layout1.findComponentById(ResourceTable.Id_list);
        ListContainer container2 =
                (ListContainer) layout2.findComponentById(ResourceTable.Id_list);
        ListContainer container3
                = (ListContainer) layout3.findComponentById(ResourceTable.Id_list);

        FilterAdapter filterAdapter1 = new FilterAdapter(getContext(), floors);
        FilterAdapter filterAdapter2 = new FilterAdapter(getContext(), clothes);
        FilterAdapter filterAdapter3 = new FilterAdapter(getContext(), sorts);
        container1.setItemProvider(filterAdapter1);
        container2.setItemProvider(filterAdapter2);
        container3.setItemProvider(filterAdapter3);

        layout1.setVisibility(Component.HIDE);
        layout2.setVisibility(Component.HIDE);
        layout3.setVisibility(Component.HIDE);

        layouts.add(layout1);
        layouts.add(layout2);
        layouts.add(layout3);

        onFilter(container1, filterAdapter1);
        onFilter(container2, filterAdapter2);
        onFilter(container3, filterAdapter3);
    }

    private void onFilter(ListContainer container, FilterAdapter filterAdapter) {
        container.setItemClickedListener((listContainer, component, i, l) -> {
            filterAdapter.setCheckItem(i);
            filterAdapter.notifyDataChanged();
            Text text = (Text) component.findComponentById(ResourceTable.Id_text);
            dropDownLayout.closeMenu();
            mTitles[index] = text.getText();
            updateTabData();
        });
    }

    private void updateTabData() {
        mTabEntities.clear();
        for (int index = 0; index < mTitles.length; index++) {
            mTabEntities.add(new TabEntity(mTitles[index], mIconSelectIds[index], mIconUnselectIds[index]));
        }
        layout.setTabData(mTabEntities);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
